package com.tsingtree.modular.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanSellAnnounce;
import com.tsingtree.modular.entity.NanSellAnnounceReply;
import com.tsingtree.modular.mapper.NanSellAnnounceReplyMapper;
import com.tsingtree.modular.model.params.NanSellAnnounceReplyParam;
import com.tsingtree.modular.model.result.NanSellAnnounceReplyResult;
import com.tsingtree.modular.service.NanSellAnnounceReplyService;
import com.tsingtree.modular.service.NanSellAnnounceService;
import com.tsingtree.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;


/**
 * <p>
 * 回复 服务实现类
 * </p>
 *
 * @author jagger
 * @since 2024-03-08
 */
@Service
public class NanSellAnnounceReplyServiceImpl extends ServiceImpl<NanSellAnnounceReplyMapper, NanSellAnnounceReply> implements NanSellAnnounceReplyService {

    @Autowired
    private NanSellAnnounceService nanSellAnnounceService;
    @Override
    public void add(NanSellAnnounceReplyParam param){
        NanSellAnnounceReply entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NanSellAnnounceReplyParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NanSellAnnounceReplyParam param){
        NanSellAnnounceReply oldEntity = getOldEntity(param);
        NanSellAnnounceReply newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NanSellAnnounceReplyResult findBySpec(NanSellAnnounceReplyParam param){
        return null;
    }

    @Override
    public List<NanSellAnnounceReplyResult> findListBySpec(NanSellAnnounceReplyParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NanSellAnnounceReplyParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(NanSellAnnounceReply reply, NanSellAnnounce nanSellAnnounce) {
        if(!this.updateById(reply)){
            throw new RuntimeException();
        }
        if(!nanSellAnnounceService.updateById(nanSellAnnounce)){
            throw new RuntimeException();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveReply(NanSellAnnounceReply reply, NanSellAnnounce sellAnnounce) {
        this.save(reply);
        if(!nanSellAnnounceService.updateById(sellAnnounce)){
            throw new RuntimeException();
        }
    }

    private Serializable getKey(NanSellAnnounceReplyParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NanSellAnnounceReply getOldEntity(NanSellAnnounceReplyParam param) {
        return this.getById(getKey(param));
    }

    private NanSellAnnounceReply getEntity(NanSellAnnounceReplyParam param) {
        NanSellAnnounceReply entity = new NanSellAnnounceReply();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
